{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import fnmatch\n",
    "import json\n",
    "import brotherhood\n",
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy.orm import sessionmaker\n",
    "from sqlalchemy.ext.declarative import declarative_base\n",
    "\n",
    "Base = declarative_base()\n",
    "from sqlalchemy import Column, Integer, String\n",
    "class Post(Base):\n",
    "    __tablename__ = 'posts'\n",
    "    id = Column(Integer, primary_key=True)\n",
    "    topic_content = Column(String(512))\n",
    "    post_content = Column(String(10000))\n",
    "    post_author = Column(String(128))\n",
    "    url = Column(String(1024))\n",
    "    flag = Column(Integer)\n",
    "    def __repr__(self):\n",
    "        return \"<Post(topic_content='%s', post_content='%s', post_author='%s', url='%s')>\" % (self.topic_content, self.post_content, self.post_author, self.url)\n",
    "\n",
    "#dbpath = 'sqlite:///db.sql'\n",
    "dbpath = 'mysql+pymysql://root:s7pXORZ123@localhost:3306/smackdown?charset=utf8'\n",
    "engine = create_engine(dbpath, echo=False, encoding='utf-8')\n",
    "Base.metadata.create_all(engine)\n",
    "Session = sessionmaker(bind=engine)\n",
    "session = Session()\n",
    "\n",
    "path = './input files'\n",
    "files = fnmatch.filter(os.listdir('./input files'), '*.json')\n",
    "for file in files:\n",
    "    data = json.load(open('/'.join([path, file])))\n",
    "    if data['forum_name'] == 'brotherhood':\n",
    "        result = brotherhood.parse(data)\n",
    "    else:\n",
    "        print 'not implemented yet'\n",
    "        \n",
    "    posts = []\n",
    "    for item in result:\n",
    "        posts.append(Post(topic_content=item['topic_content'], post_content=item['post_content'], post_author=item['post_author'], url=item['url']))\n",
    "    session.add_all(posts)\n",
    "    session.commit()\n",
    "\n",
    "print 'Done'\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
